var _allNodeIds;//全部树节点

var setting = {
    view: {
        dblClickExpand: false,
        showLine: true,
        selectedMulti: false
    },
    check: {

        enable: true,
        chkStyle: "checkbox",
        chkboxType: { "Y": "ps", "Y": "ps" }
    },
    data: {
        simpleData: {
            enable:true,
            checked: "isChecked",
            idKey: "id",
            name: "name",
            pIdKey: "pid",
            rootPId: ""
        }
    },
    callback: {
        onCheck: function (event, treeId, treeNode) {
            var treeObj = $.fn.zTree.getZTreeObj("ztree");
            var node = treeObj.getNodeByParam("id",treeNode.id);
            console.log(node);
            treeObj.expandNode(node, true, false);
        }
    }
};
function filterChild(node) {
    return (node.level > 1);
}
function filter(node) {   //过滤器直选中
    return (node.checked == true);
}
var zTreeObj;
$(function () {
   regValidate(["#mainForm"]);
    var  data =[] ;
   if(editType == 'info'){
       // 关闭checkbox的选择框
       setting.check.enable=false;
       for(var index in list){
           if(list[index].checked){
               list[index].open=true;
               data.push(list[index])
           }
       }
   }else{
       data = list;
   }
   console.log(data);
    zTreeObj = $.fn.zTree.init($("#ztree"), setting, data);

});
function doSave() {
    if(!$("#mainForm").valid()) return;//校验不通过
    var data=getFormData("#mainForm");
    if(!data.sortNum || $.trim(data.sortNum) == '') {
        data.sortNum = 999;
    }
    // if(_allNodeIds&&_allNodeIds.length>0){
    //     data.allMenuIds=_allNodeIds;
    //     var _selectedIds=getSelectedNodeIds('#ztree');//选中节点
    //     if(_selectedIds&&_selectedIds.length>0){
    //         data.menuIds=_selectedIds;
    //     }
    // }
    // ztree 选中节点
    var datalist =   $.fn.zTree.getZTreeObj('ztree').getNodesByFilter(filter);
    var selectMenuId = [];
    for(var item in datalist){
        selectMenuId.push(datalist[item].id);
    }
    data.menuIds=selectMenuId;
    var url = basePath + 'pfRole/addPfRole';
    if (editType == 'edit') {
        url = basePath + 'pfRole/editPfRole';
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(data),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success(common_save_success);
                goBack();
            }else if(data&&!data.isSuccess){
                // if (data.errorCode == 'exist') {
                //     pi_error('roleName_tag', '【' + data.roleName + '】已存在');
                //     return;
                // }

                toastr.error(data.errorDesc?data.errorDesc:common_save_fail);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function goBack() {
    var str = '&roleName=' + searchInfo.roleName + '&isvPid=' + searchInfo.isvPid + '&isvName=' + searchInfo.isvName
        + '&status=' + searchInfo.status + '&pageNo=' + searchInfo.pageNo + '&pageSize=' + searchInfo.pageSize;
    window.location.href = basePath + 'pfRole/page?t=' + new Date().getTime() + str;
}

function doBack() {
    var datalist =   $.fn.zTree.getZTreeObj('ztree').getNodesByFilter(filter);
    var selectMenuId = [],comlist=[];
    for(var item in datalist){
        selectMenuId.push(datalist[item].id);
    }
    for(var item in list){
        if (list[item].refid && list[item].refid != '') {
            comlist.push(list[item].id);
        }
    }

    if (editType != 'info' && (!checkDataChange("#mainForm",oneRoleInfo) ||!equalArray(selectMenuId,comlist))) {  //判断图片
        swal({
            title:"提示",
            text:common_no_save_msg,
            showCancelButton:true,
            showConfirmButton:true,
            confirmButtonText:"确定",
            cancelButtonText:"取消",
            animation:"slide-from-top"
        }, function() {
            goBack();
        });
        return;
    }
    goBack();
}